package service;

import dao.impl.DoctorDaoImpl;
import dao.impl.PatientDaoImpl;

/**
 * @Author:
 * @Date: 2020-04-21
 * @Description: @PACKAGE:service
 * @version: 1.0
 */
//@Deprecated
public class CheckUserPwdByRole {
    public static int check(String username, String password, String role) {
        DoctorDaoImpl doctorDao = new DoctorDaoImpl();
        PatientDaoImpl patientDao = new PatientDaoImpl();
        String databasePwd = null;
        int flag = -1;
        try {

            if (role.equals("admin")||role.equals("doctor")){
                if (username.length()==11){
                    databasePwd = (doctorDao.getByDoctorTel(username)).get(0).getPwd();
                }else if (username.length()==18){
                    databasePwd = (doctorDao.getByDoctorId(username)).get(0).getPwd();
                }

            }else if (role.equals("family")){
                if (username.length()==11){
                    databasePwd = (patientDao.getByPatientTel(username)).get(0).getPwd();
                }else if (username.length()==18){
                    databasePwd = (patientDao.getByPatientId(username)).get(0).getPwd();
                }
                if (databasePwd.trim().equals(password.trim())||databasePwd!=null) {
                    //家属身份判断
                    flag = 3;
                }

            }else if (role.equals("visitor")){
                //游客
                flag=4;
            }


            if ("doctor".equals(role)||databasePwd!=null) {
                if (databasePwd.trim().equals(password.trim())) {
                    //医生身份判断
                    flag = 2;
                    if ((doctorDao.getByDoctorId(username)).get(0).getAdmincheck().trim().equals("")) {

                    }
                    else{
                        //管理员权限判断
                        flag = 1;
                    }

                }

            }
            return flag;
        } catch (Exception e) {
            System.out.println("无该用户");
            //返回无用户
            return -2;
        }


    }
    public static String changeTelToId(String tel,String role){
        String id;
        DoctorDaoImpl doctorDao = new DoctorDaoImpl();
        PatientDaoImpl patientDao = new PatientDaoImpl();
        try{
        if(role.equals("family")){
            id=patientDao.getByPatientTel(tel).get(0).getID();
            return id;
        }else if(role.equals("doctor")){
            id=doctorDao.getByDoctorTel(tel).get(0).getId();
            return id;
        }
        }catch (Exception e){
            System.out.println("TeltoId转换错误");
        }
        return null;
    }

}
